﻿using NXDO.Data.Extension;
using NXDO.Data.Factory;
using NXDO.Data.Partition;
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TDM.DbData.DataEntity;
using TDM.WebBase;

namespace TDM.WebBiz.TestData
{
    public class DataExportHelper
    {
        /// <summary>
        /// 全量导出
        /// </summary>
        public static void ExeCreateExportData()
        {
            long id = DbGenerateSnowKey.GetSnowKey(1, 1);
            string dateFlag = DateTime.Now.ToString("yyyyMMddHHmmss");

            string sqltxt = "select * from DataExport where ExportType=0 order by ExportTime desc";
            var last = DbHelper.SetCommand(sqltxt).ExecuteEntityScalar<DataExport>();

            string exportPath = BasePathHelper.GetDataExportPath();
            //全量导出只有创建数据文件夹
            string tempDir = Path.Combine(exportPath, dateFlag);
            Directory.CreateDirectory(tempDir);//临时路径


            foreach (var item in Enum.GetNames(typeof(AllTableName)))
            {
                if (item == "DataExport" || item == "DataImport") continue;
                sqltxt = "select * from " + item;
                var cmd = DbHelper.SetCommand(sqltxt);
                NXDO.Data.DbClient.Transfer.BulkExport.GetExport(cmd, item, NXDO.Data.DbClient.Transfer.DataFileMode.Csv, tempDir).Execute();
            }

            //DataExport表
            string fileName = dateFlag + ".tdmpack";
            DataExport export = new DataExport()
            {
                ExportID = id,
                ExportTime = DateTime.Now,
                FileName = fileName,
                FilePath = id + ".tdmpack",
                ExportType = 0,
                LastExportTime = last == null ? null : last.ExportTime,
            };
            export.DBLinq().ToCommand(export).Insert();

            sqltxt = "select * from DataExport order by exportid desc limit 1";
            var cmd2 = DbHelper.SetCommand(sqltxt);
            NXDO.Data.DbClient.Transfer.BulkExport.GetExport(cmd2, "DataExport", NXDO.Data.DbClient.Transfer.DataFileMode.Csv, tempDir).Execute();

            #region  保存文件
            string zipFileName = Path.Combine(exportPath, dateFlag + ".zip");
            string storePath = Path.Combine(exportPath, id + ".tdmpack");


            if (File.Exists(zipFileName)) File.Delete(zipFileName);
            ZipFile.CreateFromDirectory(Path.Combine(exportPath, dateFlag), zipFileName);
            using (Stream stream = File.OpenRead(zipFileName))
            {
                AES_EnorDecrypt.SaveEncpSpireDocStream(storePath, stream);
            }

            try
            {
                Directory.Delete(Path.Combine(exportPath, dateFlag), true);
                File.Delete(zipFileName);
            }
            catch (Exception)
            {

            }

            #endregion
        }

        public static DataExport Get(long id)
        {
            string sqltxt = "select * from dataexport where exportid={0}";
            var rs = DbHelper.SetCommand(sqltxt, id).ExecuteEntityScalar<DataExport>();
            return rs;
        }
    }
}
